package jvm.线程安全;

import java.util.Vector;

/**
 * Vector是一个线程安全的类，但不是绝对线程安全的。
 *
 * @author scylla
 * @version V1.0
 * @project echo-java-base
 * @title
 * @package jvm.线程安全
 * @since 2022/7/6 4:12 下午
 */
public class VectorUnsafe {

    private static Vector<Integer> vectors = new Vector<>();

    public static void main(String[] args) {

        while (true) {
            for (int i = 0; i < 20; i++) {
                vectors.add(i);
            }


            Thread removeT = new Thread(() -> {
                synchronized (vectors){
                    for (int i = 0; i < vectors.size(); i++) {
                        vectors.remove(i);
                    }
                }

            });

            Thread printT = new Thread(() -> {
                synchronized (vectors){
                    for (int i = 0; i < vectors.size(); i++) {
                        System.out.println(vectors.get(i));
                    }
                }

            });

            removeT.start();
            printT.start();

            while (Thread.activeCount() > 30) ;

        }


    }

}
